package com.gwtintegration.smartrfsandbox.server.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.gwtintegration.smartrf.server.DataRequest;
import com.gwtintegration.smartrf.server.DataRequestQuery;
import com.gwtintegration.smartrfsandbox.server.data.CityDataResponse;
import com.gwtintegration.smartrfsandbox.server.entity.CityEntity;

@Repository
public class CityRepository {

	@PersistenceContext
	private EntityManager entityManager;

	public CityDataResponse fetch(DataRequest dataRequest) {
		DataRequestQuery dataRequestQuery = new DataRequestQuery(dataRequest, CityEntity.class, (Session) entityManager.getDelegate());
		return dataRequestQuery.getResult(new CityDataResponse());
	}

	public CityEntity add(CityEntity entity) {
		entityManager.persist(entity);
		return entity;
	}

	public CityEntity update(CityEntity entity) {
		entityManager.merge(entity);
		return entity;
	}

	public void remove(CityEntity entity) {
		entityManager.remove(entity);
	}
}
